#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	    int pipe_fd[2];
	        char buf[100];
		    
		    // 1. 创建管道
		         if (pipe(pipe_fd) == -1) {
		                 perror("pipe");
		                         exit(1);
		                             }
		    
		                                 // 2. 创建子进程
		                                     pid_t pid = fork();
		                                         if (pid == 0) { // 子进程读
		                                                 close(pipe_fd[1]); // 关闭写端
		                                                         read(pipe_fd[0], buf, sizeof(buf));
		                                                                 printf("Child received: %s\n", buf);
		                                                                         close(pipe_fd[0]);
		                                                                             } else { // 父进程写
		                                                                                     close(pipe_fd[0]); // 关闭读端
		                                                                                             const char *msg = "Hello from parent";
		                                                                                                     write(pipe_fd[1], msg, strlen(msg)+1);
		                                                                                                             close(pipe_fd[1]);
		                                                                                                                     wait(NULL); // 等待子进程
		                                                                                                                         }
		                                                                                                                             return 0;
		                                                                                                                             }
